package com.team11.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team11.server.mapper.SInformationMapper;
import com.team11.server.pojo.Admin;
import com.team11.server.pojo.SInformation;
import com.team11.server.pojo.TInformation;
import com.team11.server.pojo.dto.AdminLoginParam;
import com.team11.server.pojo.dto.ChangePwd;
import com.team11.server.pojo.dto.RespBean;
import com.team11.server.service.IAdminService;
import com.team11.server.service.ISInformationService;
import com.team11.server.service.ITInformationService;
import com.team11.server.service.impl.AdminServiceImpl;
import com.team11.server.service.impl.SInformationServiceImpl;
import com.team11.server.service.impl.TInformationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pjc
 * @since 2021-06-13
 */
@CrossOrigin
@RestController
@Slf4j
@Api(tags = "用户登录")
public class AdminController {
    @Resource
    private IAdminService adminService;

    @Resource
    private AdminServiceImpl adminService1;

    @Resource
    private ISInformationService isInformationService;

    @Resource
    private ITInformationService itInformationService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(AdminLoginParam adminLoginParam, HttpServletRequest request) {
        return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(),
                adminLoginParam.getRoleIds(), request);
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/admin/info")
    public Map<String, Object> getAdminInfo(@RequestParam("rId") Integer rId) {
        Map<String, Object> map = new HashMap<>();
        Admin admin = adminService1.getById(rId);
        if (admin.getTType() == 2) {
            log.info("此用户为学生");
            map = isInformationService.getStudent(admin.getRId());
        } else if (admin.getTType() == 1 || admin.getTType()==0) {
            log.info("此用户为老师或者是管理员");
            map = itInformationService.getTeacherByTJobNumber(admin.getRId());
        }
        return map;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout() {
        return RespBean.success("注销成功!");
    }

    @ApiOperation(value = "更新登录密码")
    @PostMapping("/changePassword")
    public RespBean ChangePassword(ChangePwd changePwd, HttpServletRequest request) {
        return adminService.changPasswordById(changePwd.getId(), changePwd.getNewPassword(), request);
    }


}
